/**得到年月
 * @param timestamp 时间戳*/
export function getYByTimestamp(timestamp) {
  const date = new Date(timestamp)
  const year = date.getFullYear()
  return year
}

/**得到年月
 * @param timestamp 时间戳*/
export function getYMByTimestamp(timestamp) {
  const date = new Date(timestamp)
  const seperator1 = '-'
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  const currentdate = year + seperator1 + month
  return currentdate
}

/**得到年月日
 * @param timestamp 时间戳*/
export function getYMDByTimestamp(timestamp) {
  const date = new Date(timestamp)
  const seperator1 = '-'
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  const currentdate = year + seperator1 + month + seperator1 + strDate
  return currentdate
}

/**得到时分
 * @param timestamp 时间戳*/
export function getHMByTimestamp(timestamp) {
  const date = new Date(timestamp)
  let hours = date.getHours()
  let minute = date.getMinutes()
  if (hours >= 0 && hours <= 9) {
    hours = '0' + hours
  }
  if (minute >= 0 && minute <= 9) {
    minute = '0' + minute
  }
  const currentdate = hours + ':' + minute
  return currentdate
}

/**得到年月日时分
 * @param timestamp 时间戳*/
export function getYMDHMByTimestamp(timestamp) {
  const date = new Date(timestamp)
  const seperator1 = '-'
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hours = date.getHours()
  let minute = date.getMinutes()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (day >= 0 && day <= 9) {
    day = '0' + day
  }
  if (hours >= 0 && hours <= 9) {
    hours = '0' + hours
  }
  if (minute >= 0 && minute <= 9) {
    minute = '0' + minute
  }
  const currentdate = year + seperator1 + month + seperator1 + day + ' ' + hours + ':' + minute
  return currentdate
}

/**
 * 得到年月日时分
 *  @param timestamp 时间戳
 *  */
export function getYMDHMSByTimestamp(timestamp) {
  const date = new Date(timestamp)
  const seperator1 = '-'
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hours = date.getHours()
  let minute = date.getMinutes()
  let secend = date.getSeconds()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (day >= 0 && day <= 9) {
    day = '0' + day
  }
  if (hours >= 0 && hours <= 9) {
    hours = '0' + hours
  }
  if (minute >= 0 && minute <= 9) {
    minute = '0' + minute
  }
  if (secend >= 0 && secend <= 9) {
    secend = '0' + secend
  }
  const currentdate = year + seperator1 + month + seperator1 + day + ' ' + hours + ':' + minute + ':' + secend
  return currentdate
}

/**日期格式转时间戳
 * @param date 日期格式字符串
 * */
export function getTimestampByTimestamp(date) {
  const date1 = new Date(date)
  // 有三种方式获取
  const time = date1.getTime()
  return time
}

/**时间戳2日期格式
 * @param date 日期格式字符串
 * */
export function getDateByTimestamp(date) {
  const date1 = new Date(date)
  // 有三种方式获取
  const time = date1.getTime()
  return time
}
/**得到星期数
 * @param timestamp 时间戳*/
export function getWeekByTimestamp(timestamp) {
  const date = new Date(timestamp)
  let days = date.getDay()
  switch (days) {
    case 1:
      days = '星期一'
      break
    case 2:
      days = '星期二'
      break
    case 3:
      days = '星期三'
      break
    case 4:
      days = '星期四'
      break
    case 5:
      days = '星期五'
      break
    case 6:
      days = '星期六'
      break
    case 0:
      days = '星期日'
      break

  }
  return days
}

/**
 * 最近时间计算 格式化时间。刚刚、x分钟前、x小时前、x天前、x月前、x年前
 * @param timestamp 时间戳
 * */
export function formatBeforeTime(timestamp) {
  if (timestamp <= 0) {
    return '未知'
  }

  let res = ''
  const curr = new Date().getTime()
  const tmp = parseInt((curr - timestamp) / 1000)

  const year = parseInt(tmp / (3600 * 24 * 30 * 12))
  const month = parseInt(tmp / (3600 * 24 * 30))
  const day = parseInt(tmp / (3600 * 24))
  const hour = parseInt(tmp / 3600)
  const minute = parseInt(tmp / 60)
  // const secound = parseInt(tmp % 60)
  if (year > 0) {
    res = year + '年前'
  } else if (month > 0) {
    res = month + '月前'
  } else if (day > 0) {
    res = day + '天前'
  } else if (hour > 0) {
    res = hour + '小时前'
  } else if (minute > 0) {
    res = minute + '分钟前'
  } else {
    res = '刚刚'
  }

  return res
}

/**
 * 最近时间计算 刚刚、x分钟前、x小时前、昨天、前天 、星期x、x天前、显示具体日期（精确到分）
 * @param timestamp 时间戳
 * */
export function formatBeforeTime2(timestamp) {
  if (timestamp <= 0) {
    return '未知'
  }

  //获取今天凌晨和昨天凌晨的时间戳单独判断
  const today = new Date()//今天凌晨
  today.setHours(0)
  today.setMinutes(0)
  today.setSeconds(0)

  const yesterday = new Date()//昨天凌晨
  yesterday.setDate(yesterday.getDate() - 1)
  yesterday.setHours(0)
  yesterday.setMinutes(0)
  yesterday.setSeconds(0)

  const lastYesterday = new Date()//前天凌晨
  lastYesterday.setDate(lastYesterday.getDate() - 2)
  lastYesterday.setHours(0)
  lastYesterday.setMinutes(0)
  lastYesterday.setSeconds(0)

  const day5 = new Date()//前5天
  day5.setDate(day5.getDate() - 6)
  day5.setHours(0)
  day5.setMinutes(0)
  day5.setSeconds(0)

  const curr = new Date().getTime()
  let res = ''
  const tmp = parseInt((curr - timestamp) / 1000)

  if (timestamp >= today.getTime()) {//今天内的时间
    const hour = parseInt(tmp / 3600)
    const minute = parseInt(tmp / 60)
    // const secound = tmp % 60

    if (hour > 0) {
      res = '今天' + getHMByTimestamp(timestamp)
    } else if (minute > 0) {
      res = minute + '分钟前'
    } else {
      res = '刚刚'
    }
    return res
  } else if (timestamp < today.getTime() && timestamp >= yesterday.getTime()) {//昨天的时间
    return '昨天' + getHMByTimestamp(timestamp)
  } else if (timestamp < yesterday.getTime() && timestamp >= lastYesterday.getTime()) {//前天的时间范围
    return '前天' + getHMByTimestamp(timestamp)
  } else if (timestamp < lastYesterday.getTime() && timestamp >= day5.getTime()) {//进5天内的显示星期数
    return getWeekByTimestamp(timestamp)
  } else {//其他的显示具体的日期即可
    return getYMDHMByTimestamp(timestamp)
  }

}

/**
 * 最近时间计算 x秒钟前、x分钟前、x小时前、昨天、前天 、星期x、x天前、显示具体日期（精确到分）
 * @param timestamp 时间戳
 * */
export function formatBeforeTime3(timestamp) {
  if (timestamp <= 0) {
    return '未知'
  }

  //获取今天凌晨和昨天凌晨的时间戳单独判断
  const today = new Date()//今天凌晨
  today.setHours(0)
  today.setMinutes(0)
  today.setSeconds(0)

  const yesterday = new Date()//昨天凌晨
  yesterday.setDate(yesterday.getDate() - 1)
  yesterday.setHours(0)
  yesterday.setMinutes(0)
  yesterday.setSeconds(0)

  const lastYesterday = new Date()//前天凌晨
  lastYesterday.setDate(lastYesterday.getDate() - 2)
  lastYesterday.setHours(0)
  lastYesterday.setMinutes(0)
  lastYesterday.setSeconds(0)

  const day5 = new Date()//前5天
  day5.setDate(day5.getDate() - 6)
  day5.setHours(0)
  day5.setMinutes(0)
  day5.setSeconds(0)

  const curr = new Date().getTime()
  let res = ''
  const tmp = parseInt((curr - timestamp) / 1000)

  if (timestamp >= today.getTime()) {//今天内的时间
    const hour = parseInt(tmp / 3600)
    const minute = parseInt(tmp / 60)
    const secound = tmp % 60

    if (hour > 0) {
      res = '今天' + getHMByTimestamp(timestamp)
    } else if (minute > 0) {
      res = minute + '分钟前'
    } else {
      res = secound + '秒前'
    }
    return res
  } else if (timestamp < today.getTime() && timestamp >= yesterday.getTime()) {//昨天的时间
    return '昨天' + getHMByTimestamp(timestamp)
  } else if (timestamp < yesterday.getTime() && timestamp >= lastYesterday.getTime()) {//前天的时间范围
    return '前天' + getHMByTimestamp(timestamp)
  } else if (timestamp < lastYesterday.getTime() && timestamp >= day5.getTime()) {//进5天内的显示星期数
    return getWeekByTimestamp(timestamp)
  } else {//其他的显示具体的日期即可
    return getYMDHMByTimestamp(timestamp)
  }

}

/**
 * 最近时间计算 格式化时间。今天 明天 后天  xxx月xx日
 * @param timestamp 时间戳
 * */
export function formatAfterTime(timestamp) {
  if (timestamp <= 0) {
    return '未知'
  }

  let res = ''
  //获取今天凌晨和昨天凌晨的时间戳单独判断
  const today = new Date()//今天凌晨
  today.setHours(0)
  today.setMinutes(0)
  today.setSeconds(0)

  const tomorrow = new Date()//明天凌晨
  tomorrow.setDate(tomorrow.getDate() + 1)
  tomorrow.setHours(0)
  tomorrow.setMinutes(0)
  tomorrow.setSeconds(0)

  const theDayAfTomorrow = new Date()//后天凌晨
  theDayAfTomorrow.setDate(theDayAfTomorrow.getDate() + 2)
  theDayAfTomorrow.setHours(0)
  theDayAfTomorrow.setMinutes(0)
  theDayAfTomorrow.setSeconds(0)

  const theDayAfTomorrow2 = new Date()//大后天凌晨
  theDayAfTomorrow2.setDate(theDayAfTomorrow2.getDate() + 3)
  theDayAfTomorrow2.setHours(0)
  theDayAfTomorrow2.setMinutes(0)
  theDayAfTomorrow2.setSeconds(0)

  if (timestamp >= today.getTime() && timestamp < tomorrow.getTime()) {//今天内的时间
    res = '今天' + getHMByTimestamp(timestamp)
    return res
  } else if (timestamp >= tomorrow.getTime() && timestamp < theDayAfTomorrow.getTime()) {//昨天的时间
    return '明天' + getHMByTimestamp(timestamp)
  } else if (timestamp >= theDayAfTomorrow.getTime() && timestamp < theDayAfTomorrow2.getTime()) {//前天的时间范围
    return '后天' + getHMByTimestamp(timestamp)
  } else {//其他的显示具体的日期即可
    return getYMDHMByTimestamp(timestamp)
  }
}


